package ee.fujitsu.factory;

import java.util.Date;

import ee.fujitsu.common.Type;
import ee.fujitsu.entity.Customer;
import ee.fujitsu.entity.JournalRecord;
import ee.fujitsu.entity.Movie;
import ee.fujitsu.entity.types.NewRelease;
import ee.fujitsu.entity.types.OldFilm;
import ee.fujitsu.entity.types.RegularFilm;

/**
 * A factory for creating Type objects.
 *
 * @author Andrei
 * 
 * used for switching between schema styles
 */

public class TypeFactory {

	/**
	 * Choose validation of implementation for validating xml file
	 * 
	 * Use {@link #getValidator(PropertyBean)} for create of document.
	 *
	 * @param movie the movie
	 * @param start the start
	 * @param expectedEnd the expected end
	 * @param realEnd the real end
	 * @param customer the customer
	 * @return implementation class of validation
	 */
	public JournalRecord getInitialRecord(Movie movie, Date start,
			Date expectedEnd, Date realEnd, Customer customer) {
		JournalRecord record = getRecordType(movie, start,
				expectedEnd, realEnd, customer);
		record.initPayment();
		return record;
	}
	
	/**
	 * Gets the regular record.
	 *
	 * @param movie the movie
	 * @param start the start
	 * @param expectedEnd the expected end
	 * @param realEnd the real end
	 * @param customer the customer
	 * @return the regular record
	 */
	public JournalRecord getRegularRecord(Movie movie, Date start,
			Date expectedEnd, Date realEnd, Customer customer) {
		JournalRecord record = getRecordType(movie, start,
				expectedEnd, realEnd, customer);
		record.performPayment();
		return record;
	}
	
	
	/**
	 * Gets the record type.
	 *
	 * @param movie the movie
	 * @param start the start
	 * @param expectedEnd the expected end
	 * @param realEnd the real end
	 * @param customer the customer
	 * @return the record type
	 */
	private JournalRecord getRecordType(Movie movie, Date start,
			Date expectedEnd, Date realEnd, Customer customer) {
		JournalRecord record = null;
		if (movie.getType().equals(Type.NEW_RELEASE.getType()))
			record = new NewRelease(movie, start, expectedEnd, realEnd, customer);
		else if (movie.getType().equals(Type.REGULAR_FILM.getType()))
			record = new RegularFilm(movie, start, expectedEnd, realEnd, customer);
		else if (movie.getType().equals(Type.OLD_FILM.getType()))
			record = new OldFilm(movie, start, expectedEnd, realEnd, customer);
		record.secondPayment();
		record.performBonus();
		return record;
	}

	/**
	 * Update record.
	 *
	 * @param record the record
	 * @param realEnd the real end
	 * @return the journal record
	 */
	public JournalRecord updateRecord(JournalRecord record,
			Date realEnd) {
		record.setRealEnd(realEnd);
		record.secondPayment();
		return record;
	}

}
